var gulp            = require('gulp'),
    autoprefixer    = require('gulp-autoprefixer'),
    uglify          =  require('gulp-uglify-es').default,
    rename          = require('gulp-rename'),
    del             = require('del'),
    concat          = require('gulp-concat'),
    sourcemaps      = require('gulp-sourcemaps'),
    csso            = require('gulp-csso'),
    gutil           = require('gulp-util'),
    babel           = require('gulp-babel');

var config = {
    copyFiles: [
        'mobile-lib/**/*.*',
        'mobile-tmpl/**/*.*',
        'html/**/*.*',
        'img/**/*.*',
        'mobile.html',
        'mobile-views/**/*.*'
    ],
    buildFilesFoldersRemove: [
    ]
};

// LOG ERRORS
function errorlog(err) {
    gutil.log(gutil.colors.red('[Error]'), err.toString());
}

// SCRIPT TASK
gulp.task('scripts', function () {
    return gulp.src('./mobile-sys/js/**/*')
        .pipe(sourcemaps.init())
            .pipe(uglify())
            .on('error', errorlog)
        .pipe(sourcemaps.write('../maps'))
        .pipe(gulp.dest('./build/mobile-sys/js/'));
 });

 // STYLE TASK
gulp.task('styles', function() {
    return gulp.src('./mobile-sys/css/**/*')
        .pipe(sourcemaps.init())
            .pipe(autoprefixer({
                browsers: ['last 3 versions'],
                cascade: false
            }))
            .pipe(csso())
        .pipe(sourcemaps.write('../maps'))
		.pipe(gulp.dest('./build/mobile-sys/css/'));
});

// LIB TASK
gulp.task('lib', function(){
    return gulp.src(config.copyFiles, { base: './' })
    .pipe(gulp.dest('build'));
});

// BUILD TASK
gulp.task('build:cleanfolder', function (cb) {
	del([
		'build/**'
	], cb);
});

gulp.task('build:copy', ['build:cleanfolder'], function(){
    return gulp.src(config.copyFiles, { base: './' })
    .pipe(gulp.dest('build'));
});

gulp.task('build:remove', ['build:copy'], function (cb) {
	del(config.buildFilesFoldersRemove, cb);
});

gulp.task('build', ['build:copy', 'build:remove']);

// WATCH TASK

gulp.task('watch', function () {
    gulp.watch('./mobile-sys/css/**/*', ['styles']);
    gulp.watch('./mobile-sys/js/**/*', ['scripts']);
});

gulp.task('default', ['lib', 'scripts', 'styles', 'watch']);